__author__ = '天祥'

from ParseHtml import getGrade, change, getMsg

grades = getGrade(change(getMsg()))

#计算总学分
def getTotalCredit():
    totalCredit = 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0':
            totalCredit += float(grade['credit'])
    return totalCredit

#计算必修课总学分
def getCompulsoryCredit():
    compulsory = 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0' and \
                (grade['class_type'] == '专业必修'or grade['class_type'] == '公共必修'):
            compulsory += float(grade['credit'])
    return compulsory

#计算选修课总学分
def getOptionalCredit():
    optional = 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0' and \
                (grade['class_type'] == '专业选修'or grade['class_type'] == '公共选修'):
            optional += float(grade['credit'])
    return optional

#计算专业选修课总学分
def getProOptionalCredit():
    professional = 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0' and grade['class_type'] == '专业选修':
            professional += float(grade['credit'])
    return professional

#计算公共选修课的总学分
def getPubOptionalCredit():
    public = 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0' and grade['class_type'] == '公共选修':
            public += float(grade['credit'])
    return public

#计算总学分，必修课学分，选修课学分，专业选修课学分，公共选修课学分
def getCredit():
    total, compulsory, optional, professional, public = 0, 0, 0, 0, 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0':
            total += float(grade['credit'])
            if grade['class_type'] == '专业必修'or grade['class_type'] == '公共必修':
                compulsory += float(grade['credit'])
            elif grade['class_type'] == '专业选修':
                optional += float(grade['credit'])
                professional += float(grade['credit'])
            elif grade['class_type'] == '公共选修':
                optional += float(grade['credit'])
                public += float(grade['credit'])
    return total, compulsory, optional, professional, public


#计算总绩点和所有科目的平均成绩
def cacuTotalGpa():
    gpa = 0
    credit = 0
    result, count = 0, 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0':
            gra = float(grade['grade'])         #单科成绩
            point = getPoint(gra)               #单科绩点
            tempcredit = float(grade['credit'])     #单科学分
            credit += tempcredit                    #总学分
            gpa += point * tempcredit
            result += gra                       #总成绩
            count += 1                          #课程数目
    return gpa / credit, result / count

#计算必修课绩点和所有必修课的平均成绩
def cacuCompulsoryGpa():
    gpa = 0
    credit = 0
    result, count = 0, 0
    for grade in grades:
        if grade['grade'] != None and grade['grade'] != '0.0' and \
                (grade['class_type'] == '专业必修'or grade['class_type'] == '公共必修'):
            gra = float(grade['grade'])
            point = getPoint(gra)
            tempcredit = float(grade['credit'])
            credit += tempcredit
            gpa += point * tempcredit
            result += gra                       #总成绩
            count += 1                          #课程数目
    return gpa / credit, result / count

#将成绩改成绩点
def getPoint(gra):
    point = 0
    if gra >= 90:
        point = 4
    elif gra >=85:
        point = 3.7
    elif gra >= 82:
        point = 3.3
    elif gra >= 78:
        point = 3
    elif gra >=75:
         point = 2.7
    elif gra >= 72:
        point = 2.3
    elif gra >= 68:
        point = 2
    elif gra >= 64:
        point = 1.5
    elif gra >= 60:
        point = 1
    else:
        point = 0
    return point

#计算所有信息
def cacuMsg():
    grades = getGrade(change(getMsg()))
    total, compulsory, optional, professional, public = 0, 0, 0, 0, 0
    gpa, result, count = 0, 0, 0
    cgpa,  cresult, ccount = 0, 0, 0
    for grade in grades:
        if grade['grade'] != None and float(grade['grade']) >= 60:          #成绩存在且及格
            total += float(grade['credit'])

            gra = float(grade['grade'])         #单科成绩
            point = getPoint(gra)               #单科绩点
            tempcredit = float(grade['credit'])     #单科学分
            gpa += point * tempcredit
            result += gra                       #总成绩
            count += 1                          #课程数目
            if grade['class_type'] == '专业必修'or grade['class_type'] == '公共必修':
                compulsory += float(grade['credit'])

                cgra = float(grade['grade'])         #必修课单科成绩
                cpoint = getPoint(cgra)               #必修课单科绩点
                cgpa += cpoint * float(grade['credit'])
                cresult += cgra                       #总成绩
                ccount += 1                          #课程数目
            elif grade['class_type'] == '专业选修':
                optional += float(grade['credit'])
                professional += float(grade['credit'])
            elif grade['class_type'] == '公共选修':
                optional += float(grade['credit'])
                public += float(grade['credit'])
    res = {
        '总学分：': total,
        '必修课总学分：': compulsory,
        '选修课总学分：': optional,
        '专业选修课总学分：': professional,
        '公共选修课总学分：': public,
        '总绩点：': gpa / total,
        '平均成绩：': result / count,
        '课程数目：': count,
        '必修课绩点：': cgpa / compulsory,
        '必修课平均成绩：': cresult / ccount,
        '必修课课程数目：': ccount
    }
    return res

if __name__ == '__main__':
    fout = open('result2.txt', 'w')
    res = cacuMsg()
    for x, y in res.items():
        fout.write(x)
        fout.write(str(y))
        fout.write('\r\n')